package org.cfx.auth.service.impl;

import org.cfx.auth.domain.AuthPermission;
import org.cfx.auth.service.IAuthPermissionService;
import org.cfx.auth.service.IAutoScanPermissionService;
import org.cfx.basic.annotation.Permission;
import org.cfx.basic.myenum.RequestType;
import org.cfx.basic.util.ClassUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @author RTX 9090
 */
@Service
@Transactional
public class AutoScanPermissionServiceImpl implements IAutoScanPermissionService {

    @Value("${scan-controller.base-package}")
    private String path;

    @Autowired
    private IAuthPermissionService authPermissionService;

    @Override
    public void scan() {
        //1、获取所有要解析的class集合
        List<Class> allClass = ClassUtils.getAllClassName(path);

        //删除所有permission表的数据
        authPermissionService.deleteAll();

        for (Class controller : allClass) {
            AuthPermission parent = new AuthPermission();
            RequestMapping reqAnno = (RequestMapping) controller.getAnnotation(RequestMapping.class);
            Permission perAnno = (Permission) controller.getAnnotation(Permission.class);
            //等于空，则直接结束本次循环
            if (perAnno == null) {
                continue;
            }
            if (reqAnno!=null){
                String[] value = reqAnno.value();
                if (value!=null && value.length>0){
                    //取第一个url
                    parent.setUrl(value[0]);
                }
            }
            String name = perAnno.name();
            String desc = perAnno.desc();
            String simpleName = controller.getSimpleName();
            parent.setName(name);
            parent.setDescs(desc);
            parent.setSn(simpleName);
            authPermissionService.add(parent);

            List<AuthPermission> childs = new ArrayList<>();

            Method[] methods = controller.getMethods();
            for (Method method : methods) {
                Permission annotation = method.getAnnotation(Permission.class);
                if (annotation==null) {
                    continue;
                }
                AuthPermission child = new AuthPermission();
                child.setName(annotation.name());
                child.setDescs(annotation.desc());

                child.setParentId(parent.getId());

                child.setSn(parent.getSn()+":"+method.getName());

                childs.add(child);

                /*
                 * 获取url
                 * 方案一：map
                 * 方案二：enum
                 */

                GetMapping getMapping = method.getAnnotation(GetMapping.class);
                if (getMapping != null) {
                    String[] value = getMapping.value();
                    if (value.length>0&&value!=null) {
                        child.setUrl(parent.getUrl()+value[0]);
                    }else {
                        child.setUrl(parent.getUrl());
                    }
                    continue;
                }
                PostMapping postMapping = method.getAnnotation(PostMapping.class);
                if (postMapping != null) {
                    String[] value = postMapping.value();
                    if (value.length>0&&value!=null) {
                        child.setUrl(parent.getUrl()+value[0]);
                    }else {
                        child.setUrl(parent.getUrl());
                    }
                    continue;
                }
                DeleteMapping deleteMapping = method.getAnnotation(DeleteMapping.class);
                if (deleteMapping != null) {
                    String[] value = deleteMapping.value();
                    if (value.length>0&&value!=null) {
                        child.setUrl(parent.getUrl()+value[0]);
                    }else {
                        child.setUrl(parent.getUrl());
                    }
                    continue;
                }
                PutMapping putMapping = method.getAnnotation(PutMapping.class);
                if (putMapping != null) {
                    String[] value = putMapping.value();
                    if (value.length>0&&value!=null) {
                        child.setUrl(parent.getUrl()+value[0]);
                    }else {
                        child.setUrl(parent.getUrl());
                    }
                    continue;
                }
                PatchMapping patchMapping = method.getAnnotation(PatchMapping.class);
                if (patchMapping != null) {
                    String[] value = patchMapping.value();
                    if (value.length>0&&value!=null) {
                        child.setUrl(parent.getUrl()+value[0]);
                    }else {
                        child.setUrl(parent.getUrl());
                    }
                    continue;
                }
            }
            //保存所有子权限数据
            authPermissionService.addList(childs);
        }
    }

}
